package com.nbstar.film.spider.spider;

import com.nbstar.film.spider.http.SipderStringRequest;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.filter.ReplaceBracketsNameFilter;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URLEncoder;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class BaiKeSpider extends BaseSpider  {


    public BaiKeSpider() {
        this.spiderName = "BAIKE";
        setRequest(new SipderStringRequest());
        registFilter(new ReplaceBracketsNameFilter());
    }

    @Override
    public String getContentId(String filmName) throws Exception {

        return URLEncoder.encode(filmName.replaceAll(" +", ""));
    }

    @Override
    public FilmInfo getFilmInfo(String name) throws Exception {
        String url = "https://baike.baidu.com/item/" + name;
        String html = getRequest().getHtml(url);
        //System.out.println(html);
        FilmInfo info = new FilmInfo();
        Document doc = Jsoup.parse(html);
        Elements basicInfo = doc.select("div[class^=basic-info]");
        if (basicInfo.size() == 0) {
            log.info("Can not found basic info {}", name);
            return info;
        }

        Elements basicLeft = basicInfo.get(0).getElementsByClass("basicInfo-block basicInfo-left");
        if (basicLeft.size() == 0) {
            log.info("Can not found basic left info {}", name);
            return  info;
        }

        Elements basicLeftName = basicLeft.get(0).getElementsByClass("basicInfo-item name");
        Elements basicLeftValue = basicLeft.get(0).getElementsByClass("basicInfo-item value");
        AtomicInteger i = new AtomicInteger();
        i.set(0);
        basicLeftName.forEach(v->{
            int index = i.get();
            System.out.println(v.text().trim());
            if (v.text().trim().replace((char)160 + "", "").equals("中文名")) {
                info.setCname(basicLeftValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("外文名")) {
                info.setEname(basicLeftValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("出品时间")) {
                info.setArriveDate(basicLeftValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("制片地区")) {
                info.setArriveAddress(basicLeftValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("导演")) {
                info.setDirector(basicLeftValue.get(index).text().trim());
            }

            i.getAndIncrement();
        });

        i.set(0);

        Elements basicRight = basicInfo.get(0).getElementsByClass("basicInfo-block basicInfo-right");
        Elements basicRightName = basicRight.get(0).getElementsByClass("basicInfo-item name");
        Elements basicRightValue = basicRight.get(0).getElementsByClass("basicInfo-item value");

        basicRightName.forEach(v->{
            int index = i.get();
            System.out.println("text:" + v.text());
            if (v.text().trim().replace((char)160 + "", "").equals("上映时间")) {
                info.setArriveDate(basicRightValue.get(index).text().trim());
            }


            if (v.text().trim().replace((char)160 + "", "").equals("片长")) {
                info.setFileDuration(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("地区")) {
                info.setArriveAddress(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("导演")) {
                info.setDirector(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("类型")) {
                info.setCategory(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("主演")) {
                info.setActor(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("片长")) {
                info.setFileDuration(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("集数")) {
                info.setPartNum(basicRightValue.get(index).text().trim());
            }

            if (v.text().trim().replace((char)160 + "", "").equals("每集长度")) {
                info.setFileDuration(basicRightValue.get(index).text().trim());
            }
            if (v.text().trim().replace((char)160 + "", "").equals("对白语言")) {
                info.setLanguage(basicRightValue.get(index).text().trim());
            }
            if (v.text().trim().replace((char)160 + "", "").equals("在线播放平台")) {
                info.setOnlinePlatform(basicRightValue.get(index).text().trim());
            }
            i.getAndIncrement();
        });

        Elements summary = doc.getElementsByClass("lemma-summary");
        if (summary != null) {
            info.setDesc(summary.text());
        }

        info.setSuccess(true);
        //System.out.println(html);
        return info;
    }
}
